package middle;

public class Solution_74 {

    public boolean searchMatrix(int[][] matrix, int target) {
        int start = 0, end = matrix.length * matrix[0].length-1;
        while (start <= end) {
            int middle = (start + end) >> 1;
            int row = middle / matrix[0].length;
            int col = middle % matrix[0].length;
            if (matrix[row][col] < target) {
                start = middle + 1;
            } else if (matrix[row][col] > target) {
                end = middle - 1;
            } else {
                return true;
            }
        }
        return false;
    }

    public static void main(String[] args) {
        Solution_74 model = new Solution_74();
        System.out.println(model.searchMatrix(new int[][]{{1}}, 1));
        System.out.println(model.searchMatrix(new int[][]{{1, 3, 5, 7}, {10, 11, 16, 20}, {23, 30, 34, 60}}, 3));
        System.out.println(model.searchMatrix(new int[][]{{1, 3, 5, 7}, {10, 11, 16, 20}, {23, 30, 34, 60}}, 13));
    }
}
